package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guoning.office_system.model.Function;
import com.guoning.office_system.service.RoleFunctionService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/roleFunc")
public class RoleFuncController extends BaseController {
    @Autowired
    RoleFunctionService roleFunctionService;

    /**
     * 根据角色ID获取菜单信息
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/getFuncs")
    public JsonResult getFuncs(Long roleId){
        JsonResult jsonResult;
        try {
            Map map = roleFunctionService.getFuncs(roleId);
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "查询成功", map);
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "查询异常");
        }
        return jsonResult;
    }

    /**
     * 分配权限
     * @param params
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult insert(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            Map map = JSONObject.parseObject(params, Map.class);
            String roleId = map.get("roleId").toString();
            String roleCode = map.get("roleCode").toString();
            String creator = getUserDate(request).getUserName();
            List<Function> funcs = JSONArray.parseArray(map.get("funcList").toString(), Function.class);
            Criteria criteria = new Criteria();
            criteria.addExtField("roleId", roleId);
            criteria.addExtField("roleCode", roleCode);
            criteria.addExtField("creator", creator);
            criteria.addExtField("funcList", funcs);
            int i = roleFunctionService.insert(criteria);
            if(i == 0){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "操作失败");
                return jsonResult;
            }
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }

    /**
     * 取消关联权限
     * @param params
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JsonResult delete(HttpServletRequest request, String params){
        JsonResult jsonResult;
        try {
            Map map = JSONObject.parseObject(params, Map.class);
            String roleId = map.get("roleId").toString();
            String roleCode = map.get("roleCode").toString();
            String modifier = getUserDate(request).getUserName();
            List<Function> funcs = JSONArray.parseArray(map.get("funcList").toString(), Function.class);
            Criteria criteria = new Criteria();
            criteria.addExtField("roleId", roleId);
            criteria.addExtField("roleCode", roleCode);
            criteria.addExtField("modifier", modifier);
            criteria.addExtField("funcList", funcs);
            int i = roleFunctionService.updateForDelete(criteria);
            if(i == 0){
                jsonResult = new JsonResult(Constants.STATUS_FAIL, "操作失败");
                return jsonResult;
            }
            jsonResult = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception e){
            jsonResult = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return jsonResult;
    }
}
